

package com.mt.repair.controller.repairManagement;

import com.mt.common.core.web.JsonResult;
import org.springframework.security.access.prepost.PreAuthorize;
import com.mt.common.core.annotation.ApiPageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mt.common.core.web.base.PageDTO;
import com.mt.common.core.web.base.PageResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.mt.repair.dto.repairManagement.MessageEditDto;
import com.mt.repair.entity.repairManagement.Message;
import com.mt.repair.service.repairManagement.MessageService;
import com.mt.common.system.service.UserService;
import com.mt.common.system.service.UserService;


@Api(tags = "消息中心")
@RestController
@RequestMapping("/api/repair/repairManagement/Message")
@CrossOrigin(allowCredentials = "true")
public class MessageController {
    private static Logger logger = LoggerFactory.getLogger(MessageController.class);


    @Autowired
    private MessageService messageService;
    @Autowired
    private UserService userService;

    /**
     * 根据分页参数查询消息中心集合
     *
     * @param pageDTO 分页条件
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:Message:view')")
    @ApiOperation("分页查询消息中心")
    @ApiPageParam
    @PostMapping("/findMessages")
    public PageResultDTO findMessages(@RequestBody PageDTO pageDTO) {
        return this.messageService.findMessages(pageDTO);
    }

    /**
     * 根据ID查询指定的消息中心
     *
     * @param messageId Id
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:Message:view')")
    @ApiOperation("根据id查询消息中心")
    @ApiPageParam
    @PostMapping("/findMessage")
    public Message findMessage(@RequestParam Long messageId) {
        return this.messageService.findMessage(messageId);
    }

    /**
     * 根据ID查询指定的消息中心(包含外键名称)
     *
     * @param messageId Id
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:Message:view')")
    @ApiOperation("根据ID查询指定的消息中心(包含外键名称)")
    @PostMapping("/findMessageForView")
    public Message findMessageForView(@RequestParam Long messageId) {
        return this.messageService.findMessageWithForeignName(messageId);
    }

    /**
     * 根据ID查询指定的消息中心(包含消息中心和外键名称)
     *
     * @param messageId Id
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:Message:view')")
    @ApiOperation("根据ID查询指定的消息中心(包含消息中心和外键名称)")
    @PostMapping("/findMessageForEdit")
    public MessageEditDto findMessageForEdit(@RequestParam Long messageId) {
        MessageEditDto messageEditDto = new MessageEditDto();
        messageEditDto.setMessage(this.messageService.findMessageWithForeignName(messageId));

        this.prepareMessageEditDto(messageEditDto);

        return messageEditDto;
    }

    /**
     * 根据ID查询指定的消息中心(只提取ID 和 Name)
     *
     * @param messageId Id
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:Message:view')")
    @ApiOperation("根据ID查询指定的消息中心(只提取ID 和 Name)")
    @PostMapping("/findMessagesWithIdNameById")
    public Message findMessagesWithIdNameById(@RequestParam Long messageId) {
        return this.messageService.findMessagesWithIdNameById(messageId);
    }

    /**
     * 根据名称查询消息中心集合(只提取ID 和 Name)
     *
     * @param messageName 名称
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:Message:view')")
    @ApiOperation("根据名称查询消息中心集合(只提取ID 和 Name)")
    @PostMapping("/findMessagesWithIdNameByName")
    public List<Message> findMessagesWithIdNameByName(@RequestParam String messageName) {
        return this.messageService.findMessagesWithIdNameByName(messageName);
    }


    /**
     * 创建新的消息中心
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:Message:add')")
    @ApiOperation("创建新的消息中心")
    @PostMapping("/createMessage")
    public MessageEditDto createMessage() {
        MessageEditDto messageEditDto = new MessageEditDto();
        messageEditDto.setMessage(new Message());

        this.prepareMessageEditDto(messageEditDto);
        return messageEditDto;
    }

    private void prepareMessageEditDto(MessageEditDto messageEditDto) {
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        messageEditDto.setToUserUsers(this.userService.findAllUsersWithIdName());
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        messageEditDto.setFromUserUsers(this.userService.findAllUsersWithIdName());
    }

    /**
     * 新增保存消息中心
     *
     * @param message 实体对象
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:Message:add')")
    @ApiOperation("新增保存消息中心")
    @PostMapping("/saveMessage")
    public Message saveMessage(@RequestBody Message message) {
        return this.messageService.saveMessage(message);
    }

    /**
     * 修改保存消息中心
     *
     * @param message 实体对象
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:Message:update')")
    @ApiOperation("修改保存消息中心")
    @PostMapping("/updateMessage")
    public Message updateMessage(@RequestBody Message message) {
        return this.messageService.updateMessage(message);
    }

    /**
     * 根据ID删除消息中心
     *
     * @param messageId ID
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:Message:remove')")
    @ApiOperation("根据ID删除消息中心")
    @PostMapping("/deleteMessage")
    public void deleteMessage(@RequestParam Long messageId) {
        this.messageService.deleteMessage(messageId);
    }


    @ApiOperation("根据toUser查询未读消息数量")
    @PostMapping("/countNotReadNum")
    public Long countNotReadNum(@RequestParam Long toUserId) {
        return this.messageService.countNotReadNum(toUserId);
    }
    @ApiOperation("阅读消息")
    @PostMapping("/readMessage")
    public JsonResult readMessage(@RequestParam Long messageId) {
        return this.messageService.readMessage(messageId);
    }

}

